<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-material.min.css">
  <script src="/lib/pace/pace.min.js"></script>


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('https://ychienhung.gitee.io').hostname,
    root: '/',
    scheme: 'Muse',
    version: '7.6.0',
    exturl: false,
    sidebar: {"position":"right","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":true,"show_result":true,"style":"mac"},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="什么叫规范？​        在C语言中不遵守编译器的规定，编译器在编译时就会报错，这个规定叫作规则。但是有一种规定，它是一种人为的、约定成俗的，即使不按照那种规定也不会出错，这种规定就叫作规范。 ​        虽然我们不按照规范也不会出错，但是那样代码写得就会很乱。大家刚开始学习C语言的时候，第一步不是说要把程序写正确，而是要">
<meta property="og:type" content="article">
<meta property="og:title" content="C语言代码规范（编程规范）">
<meta property="og:url" content="https://ychienhung.gitee.io/2019/12/28/C%E8%AF%AD%E8%A8%80%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83%EF%BC%88%E7%BC%96%E7%A8%8B%E8%A7%84%E8%8C%83%EF%BC%89/index.html">
<meta property="og:site_name" content="YChienHung&#39;s blog">
<meta property="og:description" content="什么叫规范？​        在C语言中不遵守编译器的规定，编译器在编译时就会报错，这个规定叫作规则。但是有一种规定，它是一种人为的、约定成俗的，即使不按照那种规定也不会出错，这种规定就叫作规范。 ​        虽然我们不按照规范也不会出错，但是那样代码写得就会很乱。大家刚开始学习C语言的时候，第一步不是说要把程序写正确，而是要">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2019-12-28T00:00:50.000Z">
<meta property="article:modified_time" content="2019-12-29T10:05:03.169Z">
<meta property="article:author" content="YChienHung">
<meta property="article:tag" content="C">
<meta property="article:tag" content="代码规范">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://ychienhung.gitee.io/2019/12/28/C%E8%AF%AD%E8%A8%80%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83%EF%BC%88%E7%BC%96%E7%A8%8B%E8%A7%84%E8%8C%83%EF%BC%89/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>
  <title>C语言代码规范（编程规范） | YChienHung's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="YChienHung's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">YChienHung's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-memoir">

    <a href="/memoir/" rel="section"><i class="fa fa-fw fa-sitemap"></i>回忆录</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ychienhung.gitee.io/2019/12/28/C%E8%AF%AD%E8%A8%80%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83%EF%BC%88%E7%BC%96%E7%A8%8B%E8%A7%84%E8%8C%83%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="YChienHung">
      <meta itemprop="description" content="Forget me not.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="YChienHung's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          C语言代码规范（编程规范）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2019-12-28 08:00:50" itemprop="dateCreated datePublished" datetime="2019-12-28T08:00:50+08:00">2019-12-28</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-12-29 18:05:03" itemprop="dateModified" datetime="2019-12-29T18:05:03+08:00">2019-12-29</time>
              </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="什么叫规范？"><a href="#什么叫规范？" class="headerlink" title="什么叫规范？"></a>什么叫规范？</h2><p>​        在<a href="http://c.biancheng.net/c/" target="_blank" rel="noopener">C语言</a>中不遵守编译器的规定，编译器在编译时就会报错，这个规定叫作规则。但是有一种规定，它是一种人为的、约定成俗的，即使不按照那种规定也不会出错，这种规定就叫作规范。</p>
<p>​        虽然我们不按照规范也不会出错，但是那样代码写得就会很乱。大家刚开始学习C语言的时候，第一步不是说要把程序写正确，而是要<a id="more"></a>写规范。因为如果你养成一种非常不好的写代码的习惯，代码就会写得乱七八糟，等到将来工作面试的时候，这样的习惯可能会让你失去机会。</p>
<h2 id="代码如何写才能规范"><a href="#代码如何写才能规范" class="headerlink" title="代码如何写才能规范"></a><strong>代码如何写才能规范</strong></h2><p>​        那么代码如何写才能写得很规范呢？代码的规范化不是说看完本节内容后就能实现的。它里面细节很多，而且需要不停地写代码练习，不停地领悟，慢慢地才能掌握的一种编程习惯。所以大家不要想着一下子就能把代码规范化的所有知识全部掌握，也不要想着一下子就能把代码写规范，这是不太可能的。</p>
<p>​        有很多知识，比如为什么代码要这样写，为什么不能那样写，作为一个初学者你是很难弄明白的。有很多规范是为了在程序代码量很大的时候，便于自己阅读，也便于别人阅读。</p>
<p>​        所以刚开始的时候有很多规范你不知道为什么要那样规定，你就单纯地模仿就行了。等将来敲代码敲得时间长了，你就会感觉到那样写是很有好处的。</p>
<h2 id="代码规范化的好处"><a href="#代码规范化的好处" class="headerlink" title="代码规范化的好处"></a><strong>代码规范化的好处</strong></h2><p>​        代码规范化的第一个好处就是看着很整齐、很舒服。假如你现在用不规范的方式写了一万行代码，现在能看得懂，但等过了三个月你再回头看时就很吃力了，更不要说给别人看了。所以代码要写规范，比如加注释就是代码规范化的一个思想。</p>
<p>​        在一般情况下，根据软件工程的思想，我们的注释要占整个文档的20%以上。所以注释要写得很详细，而且格式要写得很规范。</p>
<p>​        第二个好处是，把代码写规范则程序不容易出错。如果按照不规范的格式输入代码的话，很容易出错。而代码写规范的话即使出错了查错也会很方便。格式虽然不会影响程序的功能，但会影响可读性。程序的格式追求清晰、美观，是程序风格的重要构成元素。</p>
<h3 id="代码规范化的七大原则"><a href="#代码规范化的七大原则" class="headerlink" title="代码规范化的七大原则"></a><strong>代码规范化的七大原则</strong></h3><p>代码规范化基本上有七大原则，体现在空行、空格、成对书写、缩进、对齐、代码行、注释七方面的书写规范上。</p>
<ol>
<li><h4 id="空行"><a href="#空行" class="headerlink" title="空行"></a><strong>空行</strong></h4></li>
</ol>
<p>空行起着分隔程序段落的作用。空行得体将使程序的布局更加清晰。空行不会浪费内存，虽然打印含有空行的程序会多消耗一些纸张，但是值得。</p>
<p> 规则一：定义变量后要空行。尽可能在定义变量的同时初始化该变量，即遵循就近原则。如果变量的引用和定义相隔比较远，那么变量的初始化就很容易被忘记。若引用了未被初始化的变量，就会导致程序出错。</p>
<p> 规则二：每个函数定义结束之后都要加空行。</p>
<p> 总规则：两个相对独立的程序块、变量说明之后必须要加空行。比如上面几行代码完成的是一个功能，下面几行代码完成的是另一个功能，那么它们中间就要加空行。这样看起来更清晰。</p>
<ol start="2">
<li><h4 id="空格"><a href="#空格" class="headerlink" title="空格"></a><strong>空格</strong></h4></li>
</ol>
<p>规则一：关键字之后要留空格。像 const、case 等关键字之后至少要留一个空格，否则无法辨析关键字。像 if、for、while 等关键字之后应留一个空格再跟左括号(，以突出关键字。</p>
<p> 规则二：函数名之后不要留空格，应紧跟左括号(，以与关键字区别。</p>
<p> 规则三：(向后紧跟；)、,、;这三个向前紧跟；紧跟处不留空格。</p>
<p> 规则四：,之后要留空格。如果;不是一行的结束符号，其后要留空格。</p>
<p> 规则五：赋值运算符、关系运算符、算术运算符、逻辑运算符、位运算符，如 =、==、！=、+=、－=、<em>=、/=、%=、&gt;&gt;=、&lt;&lt;=、&amp;=、^=、|=、&gt;、&lt;=、&gt;、&gt;=、+、－、</em>、/、%、&amp;、|、&amp;&amp;、||、&lt;&lt;、&gt;&gt;、^ 等双目运算符的前后应当加空格。</p>
<p> 注意，运算符“%”是求余运算符，与 printf 中 %d 的“%”不同，所以 %d 中的“%”前后不用加空格。</p>
<p> 规则六：单目运算符 !、~、++、－－、－、*、&amp; 等前后不加空格。</p>
<p> 注意：</p>
<p>·    这里的“－”和规则五里面的“－”不同。这里的“－”是负号运算符，规则五里面的“－”是减法运算符。</p>
<p>·    这里的“<em>”和规则五里面的“</em>”也不同。这里的“<em>”是指针运算符，规则五里面的“</em>”是乘法运算符。</p>
<p>·    这里的“&amp;”和规则五里面的“&amp;”也不同。这里的“&amp;”是取地址运算符，规则五里面的“&amp;”是按位与运算符。</p>
<p> 总之，规则六中的是单目运算符，而规则五中的是双目运算符，它们是不一样的。</p>
<p> 规则七：像数组符号[]、结构体成员运算符.、指向结构体成员运算符-&gt;，这类操作符前后不加空格。</p>
<p> 规则八：对于表达式比较长的 for 语句和 if 语句，为了紧凑起见，可以适当地去掉一些空格。但 for 和 if 后面紧跟的空格不可以删，其后面的语句可以根据语句的长度适当地去掉一些空格。例如：</p>
<p>for (i=0; i&lt;10; i++)</p>
<p>for 和分号后面保留空格就可以了，=和&lt;前后的空格可去掉。</p>
<ol start="3">
<li><h4 id="成对书写"><a href="#成对书写" class="headerlink" title="成对书写"></a><strong>成对书写</strong></h4></li>
</ol>
<p>成对的符号一定要成对书写，如 ()、{}。不要写完左括号然后写内容最后再补右括号，这样很容易漏掉右括号，尤其是写嵌套程序的时候。</p>
<ol start="4">
<li><h4 id="缩进"><a href="#缩进" class="headerlink" title="缩进"></a><strong>缩进</strong></h4></li>
</ol>
<p>缩进是通过键盘上的 Tab 键实现的，缩进可以使程序更有层次感。原则是：如果地位相等，则不需要缩进；如果属于某一个代码的内部代码就需要缩进。</p>
<ol start="5">
<li><h4 id="对齐"><a href="#对齐" class="headerlink" title="对齐"></a><strong>对齐</strong></h4></li>
</ol>
<p>对齐主要是针对大括号{}说的：</p>
<p> 规则一：{和}分别都要独占一行。互为一对的{和}要位于同一列，并且与引用它们的语句左对齐。</p>
<p> 规则二：{}之内的代码要向内缩进一个 Tab，且同一地位的要左对齐，地位不同的继续缩进。</p>
<p> 还有需要注意的是，很多编程软件是会“自动对齐”的，比如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (…)</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>写完 if 那一行后，按回车，此时光标在括号的右边，而此 if 下的大括号要写在与 if 左对齐的正下方，通常我们是按一下 Backspace 键使光标停在与 if 左对齐的正下方。但事实上我们不需要这样做，我们直接输入大括号即可，系统会自动对齐到与 if 左对齐的正下方。再接着看：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span> (…)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">while</span> (…)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>写完 while 那一行后，按回车，此时光标不是停在与 while 左对齐的正下方，同样，我们不需要按 Backspace，直接输入大括号即可，系统会自动对齐到与 while 左对齐的正下方的。</p>
<p> 此外编程软件还有“对齐、缩进修正”功能。就是按 Ctrl+A 全选，然后按 Alt+F8，这时程序中所有成对的大括号都会自动对齐，未缩进的也会自动缩进。不管是在编程过程中，还是在编写结束完之后，都可以使用这个技巧。但如果完全按照规范写，那根本就不需要这个技巧，所以，这只是一个辅助功能。</p>
<ol start="6">
<li><h4 id="代码行"><a href="#代码行" class="headerlink" title="代码行"></a><strong>代码行</strong></h4></li>
</ol>
<p>规则一：一行代码只做一件事情，如只定义一个变量，或只写一条语句。这样的代码容易阅读，并且便于写注释。</p>
<p> 规则二：if、else、for、while、do 等语句自占一行，执行语句不得紧跟其后。此外，非常重要的一点是，不论执行语句有多少行，就算只有一行也要加{}，并且遵循对齐的原则，这样可以防止书写失误。</p>
<ol start="7">
<li><h4 id="注释"><a href="#注释" class="headerlink" title="注释"></a><strong>注释</strong></h4></li>
</ol>
<p>C语言中一行注释一般采用//…，多行注释必须采用/<em>…</em>/。注释通常用于重要的代码行或段落提示。在一般情况下，源程序有效注释量必须在 20% 以上。虽然注释有助于理解代码，但注意不可过多地使用注释。</p>
<p> 规则一：注释是对代码的“提示”，而不是文档。程序中的注释不可喧宾夺主，注释太多会让人眼花缭乱。</p>
<p> 规则二：如果代码本来就是清楚的，则不必加注释。例如：</p>
<p>i++; //i加1</p>
<p>这个就是多余的注释。</p>
<p> 规则三：边写代码边注释，修改代码的同时要修改相应的注释，以保证注释与代码的一致性，不再有用的注释要删除。</p>
<p> 规则四：当代码比较长，特别是有多重嵌套的时候，应当在段落的结束处加注释，这样便于阅读。</p>
<p> 规则五：每一条宏定义的右边必须要有注释，说明其作用。</p>
<h2 id="实际案例"><a href="#实际案例" class="headerlink" title="实际案例"></a><strong>实际案例</strong></h2><p>下面我们给出一段求一元二次方程的代码，让大家实际感受一下。虽然这个程序不包含所有的规范，但一些包含的规范我们可以对照着看看。</p>
<p> 以下程序的功能是把任何一个一元二次方程 ax2+bx+c=0 的解给求出来。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"># <span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta"># <span class="meta-keyword">include</span> <span class="meta-string">&lt;math.h&gt; /*因为要用到求平方函数sqrt()，所以要包含头文件 math.h*/</span></span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//把三个系数保存到计算机中</span></span><br><span class="line">	<span class="keyword">int</span> a = <span class="number">1</span>; <span class="comment">// “=”不表示相等，而是表示赋值</span></span><br><span class="line">	<span class="keyword">int</span> b = <span class="number">2</span>;</span><br><span class="line">	<span class="keyword">int</span> c = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">double</span> delta;  <span class="comment">//delta存放的是b*b - 4*a*c的值</span></span><br><span class="line">	<span class="keyword">double</span> x1, x2; <span class="comment">//分别用于存放一元二次方程的两个解</span></span><br><span class="line"></span><br><span class="line">	delta = b*b - <span class="number">4</span>*a*c;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span> (delta &gt; <span class="number">0</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		x1 = (-b + <span class="built_in">sqrt</span>(delta)) / (<span class="number">2</span>*a);</span><br><span class="line"> 		x2 = (-b - <span class="built_in">sqrt</span>(delta)) / (<span class="number">2</span>*a);</span><br><span class="line"></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"该一元二次方程有两个解，x1 = %f, x2 = %f\n"</span>, x1, x2);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">0</span> == delta)</span><br><span class="line">	&#123;</span><br><span class="line">		x1 = (-b) / (<span class="number">2</span>*a);</span><br><span class="line">		x2 = x1; <span class="comment">//左边值赋给右边</span></span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"该一元二次方程有一个唯一解，x1 = x2 = %f\n"</span>, x1);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">"无解\n"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行结果：<br> 该一元二次方程有一个唯一解，x1 = x2 = -1.000000</p>
<p> 大家不用关心程序的逻辑和思路，只关注代码规范即可。</p>
<p><strong>小结</strong></p>
<p>单纯地读完一遍肯定没什么感觉，没关系，要想把代码写规范不是一朝一夕的事，这应该是一种习惯，需要不停地敲、不停的积累。刚开始的时候多模仿，照着规范写就行了，以后在写代码的时候，情不自禁地就会按规范的方式书写了。</p>

    </div>

    
    
    
        <div class="reward-container">
  <div></div>
  <button disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="YChienHung 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="YChienHung 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>YChienHung
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://ychienhung.gitee.io/2019/12/28/C%E8%AF%AD%E8%A8%80%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83%EF%BC%88%E7%BC%96%E7%A8%8B%E8%A7%84%E8%8C%83%EF%BC%89/" title="C语言代码规范（编程规范）">https://ychienhung.gitee.io/2019/12/28/C语言代码规范（编程规范）/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/C/" rel="tag"><i class="fa fa-tag"></i> C</a>
              <a href="/tags/%E4%BB%A3%E7%A0%81%E8%A7%84%E8%8C%83/" rel="tag"><i class="fa fa-tag"></i> 代码规范</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/12/27/%E5%8D%95%E7%89%87%E6%9C%BA%E5%9F%B9%E8%AE%AD%E6%97%A5%E5%BF%97/" rel="prev" title="单片机培训日志">
      <i class="fa fa-chevron-left"></i> 单片机培训日志
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/12/28/Keil5-%E9%97%AE%E9%A2%98%E5%A4%84%E7%90%86%E6%96%B9%E6%A1%88%E5%90%88%E9%9B%86/" rel="next" title="Keil5 问题处理方案合集">
      Keil5 问题处理方案合集 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC8zOTIxMi8xNTczOQ=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么叫规范？"><span class="nav-number">1.</span> <span class="nav-text">什么叫规范？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码如何写才能规范"><span class="nav-number">2.</span> <span class="nav-text">代码如何写才能规范</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码规范化的好处"><span class="nav-number">3.</span> <span class="nav-text">代码规范化的好处</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#代码规范化的七大原则"><span class="nav-number">3.1.</span> <span class="nav-text">代码规范化的七大原则</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#空行"><span class="nav-number">3.1.1.</span> <span class="nav-text">空行</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#空格"><span class="nav-number">3.1.2.</span> <span class="nav-text">空格</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#成对书写"><span class="nav-number">3.1.3.</span> <span class="nav-text">成对书写</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#缩进"><span class="nav-number">3.1.4.</span> <span class="nav-text">缩进</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#对齐"><span class="nav-number">3.1.5.</span> <span class="nav-text">对齐</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#代码行"><span class="nav-number">3.1.6.</span> <span class="nav-text">代码行</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#注释"><span class="nav-number">3.1.7.</span> <span class="nav-text">注释</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#实际案例"><span class="nav-number">4.</span> <span class="nav-text">实际案例</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="YChienHung"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">YChienHung</p>
  <div class="site-description" itemprop="description">Forget me not.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">18</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/ychienhung" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ychienhung" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/ychienhung" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;ychienhung" rel="noopener" target="_blank"><i class="fa fa-fw fa-gitee"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/6664907998" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;6664907998" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1399449399@qq.com" title="E-Mail → mailto:1399449399@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://diygod.me/" title="https:&#x2F;&#x2F;diygod.me" rel="noopener" target="_blank">DIYGod</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">YChienHung</span>
</div>

<!-- 在网页底部添加网站运行时间 -->
<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
    var now = new Date();
    function createtime() {
        var grt= new Date("07/20/2019 19:21:37");//此处修改你的建站时间或者网站上线时间
        now.setTime(now.getTime()+250);
        days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
        if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
        mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
        snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
        document.getElementById("timeDate").innerHTML = "本站已经运行 "+dnum+" 天 ";
        document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
    }
setInterval("createtime()",250);
</script>
<br>

托管于<a href="https://gitee.com/" target="_blank" rel="nofollow"> Gitee</a>


  <span class="post-meta-divider">|</span>

  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.6.0
  </div>

<!-- 新增访客统计代码-->
<div class="busuanzi-count">
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
  <span id="busuanzi_container_site_pv">本站已被访问 <span id="busuanzi_value_site_pv"></span>次</span>
</div>
<!-- 新增访客统计代码 END-->

<!-- 页面点击小红心 -->
<script type="text/javascript" src="\js\love.js"></script>
<!--浏览器搞笑标题-->
<!--<script type="text/javascript" src="\js\FunnyTitle.js"></script>-->

<!-- require APlayer -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">
<script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<!-- require MetingJS -->
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

<!--MusicPlayer-->
<meting-js
  fixed ="true"
	server="tencent"
	type  ="playlist"
	id    ="3578030853"
  loop	="all"
  order	="list"
  listFolded="false">
</meting-js>
        








      </div>
    </footer>
  </div>

  
  <script size="150" alpha="0.6" zIndex="-2" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-ribbon@1/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

</body>
</html>
